package org.easy.db;

import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: wxl
 * @CreateTime: 2022/10/27 17:25
 */
@Slf4j
public class DataBaseOperation implements IDataBaseOperation {

    private org.easy.db.DataBaseConnectTemplate dataBaseConnectTemplate;

    public DataBaseOperation (DataBaseConnectTemplate dataBaseConnectTemplate) {
        this.dataBaseConnectTemplate = dataBaseConnectTemplate;
    }

    /**
     * 根据数据库名称查找所有的表
     *
     * @param dbName 数据库名
     * @return List<String>
     */
    @Override
    public Map<String,List<String>> queryTableNamesByDbName(String dbName) {
        JdbcTemplate template = this.dataBaseConnectTemplate.getJdbcTemplate();
        List<Map<String,Object>> list = template.queryForList(org.easy.db.SqlUtil.getTableNameSql(org.easy.db.Type.MYSQL,dbName));
        List<String> tableNameList = list.stream().map(m -> (String)m.get("table_name")).collect(Collectors.toList());
        Map<String,List<String>> resultMap = new HashMap<>(50);
        resultMap.put(dbName,tableNameList);
        return resultMap;
    }

    /**
     * 创建表结构
     *
     * @param sql 表结构语句
     * @return boolean
     */
    @Override
    public boolean createTable(String sql) {
        JdbcTemplate template = this.dataBaseConnectTemplate.getJdbcTemplate();
        try {
            template.execute(sql);
            return true;
        } catch (DataAccessException e) {
            log.info("执行创建表结构SQL出现异常:{}",e.getMessage());
            throw e;
        }
    }

    /**
     * 查询
     *
     * @param sql 查询语句
     * @return List<Map < String, Object>>
     */
    @Override
    public List<Map<String, Object>> query(String sql) {
        JdbcTemplate template = this.dataBaseConnectTemplate.getJdbcTemplate();
        try {
            return template.queryForList(sql);
        }  catch (DataAccessException e) {
            log.info("执行查询SQL出现异常:{}",e.getMessage());
            throw e;
        }
    }

    /**
     * 更新
     *
     * @param sql 更新语句
     * @return int
     */
    @Override
    public int update(String sql) {
        JdbcTemplate template = this.dataBaseConnectTemplate.getJdbcTemplate();
        try {
            return template.update(sql);
        }  catch (DataAccessException e) {
            log.info("执行更新SQL出现异常:{}",e.getMessage());
            throw e;
        }
    }

    /**
     * 删除
     *
     * @param sql 删除语句
     * @return int
     */
    @Override
    public int delete(String sql) {
        JdbcTemplate template = this.dataBaseConnectTemplate.getJdbcTemplate();
        try {
            template.execute(sql);
            return 1;
        }  catch (DataAccessException e) {
            log.info("执行删除SQL出现异常:{}",e.getMessage());
            throw e;
        }
    }

    /**
     * alter
     *
     * @param sql alter语句
     */
    @Override
    public void alter(String sql) {
        JdbcTemplate template = this.dataBaseConnectTemplate.getJdbcTemplate();
        try {
            template.execute(sql);
        }  catch (DataAccessException e) {
            log.info("执行alter的SQL出现异常:{}",e.getMessage());
            throw e;
        }
    }

    /**
     * create
     *
     * @param sql alter语句
     */
    @Override
    public void create(String sql) {
        JdbcTemplate template = this.dataBaseConnectTemplate.getJdbcTemplate();
        try {
            template.execute(sql);
        }  catch (DataAccessException e) {
            log.info("执行create的SQL出现异常:{}",e.getMessage());
            throw e;
        }
    }

    public void setDataBaseConnectTemplate(DataBaseConnectTemplate dataBaseConnectTemplate) {
        this.dataBaseConnectTemplate = dataBaseConnectTemplate;
    }

    public DataBaseConnectTemplate getDataBaseConnectTemplate() {
        return dataBaseConnectTemplate;
    }
}
